package com.zbkj.crmeb.store.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crmeb.core.common.CommonResult;
import com.crmeb.core.config.RestTemplateConfig;
import com.utils.RestTemplateUtil;
import com.utils.ThreadPoolUtils;
import com.utils.monitor.DingMsgEnum;
import com.utils.monitor.DingTalkMsgUtil;
import com.zbkj.crmeb.order.StoreOrderFactory;
import com.zbkj.crmeb.store.dao.StoreOrderDao;
import com.zbkj.crmeb.store.model.StoreOrder;
import com.zbkj.crmeb.store.service.CommonStoreOrderService;
import com.zbkj.crmeb.store.service.StoreProductAttrValueService;
import com.zbkj.crmeb.store.service.StoreProductService;
import com.zbkj.crmeb.store.vo.StoreOrderInfoVo;
import com.zbkj.crmeb.task.product.junmeng.dto.WarehouseResultData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

/**
 * 凯仓订单推送
 * @author daigb
 * @date:  2022-10-18 13:45
 */
@Slf4j
@Service("commonNewStoreOrderServiceImpl")
public class CommonNewStoreOrderServiceImpl implements CommonStoreOrderService {
    @Autowired
    RestTemplateUtil restTemplateUtil;
    @Resource
    private StoreOrderDao storeOrderDao;
    @Autowired
    RestTemplateConfig restTemplateConfig;
    @Autowired
    StoreProductAttrValueService storeProductAttrValueService;
    @Autowired
    StoreProductService storeProductService;
    @Autowired
    StoreOrderFactory storeOrderFactory;

    private static final String charset = "UTF-8";
    private static final String signType = "MD5";
	private static final String customerNo = "jhy3nrg2ugoxwsov"; //客户编码
	private static final String key = "qjvlulgnldfgix5w4vt2s876yfsqz9an"; // 秘钥
	private static final String url = "http://api.jmcompany.cn/wzbc/api/openapi/main.html";

	private static final Map<String,String> PAY_TYPE_MAP = new HashMap<>();
	static {
		PAY_TYPE_MAP.put("ALIPAY","31222699S7");
		PAY_TYPE_MAP.put("WECHAT","4403169D3W");
	}

	@Override
	public CommonResult personalQuota(StoreOrder storeOrder) {
		// 组装参数
		JSONObject params = new JSONObject();
		params.put("warehouse_no", "WZBCN");
		params.put("charset", charset);
		params.put("customer_no", customerNo);
		params.put("nonce_str", UUID.randomUUID().toString().replaceAll("-", ""));
		params.put("method", "personal_quota");
		params.put("sign_type", signType);
		params.put("name", StringUtils.defaultString(storeOrder.getPayName(),storeOrder.getRealName()));
		params.put("idCard",storeOrder.getIdCard());

		// 组装加签参数
		String signStr = "charset=" + params.getString("charset") +
				"&" + "customer_no=" + params.getString("customer_no") +
				"&" + "method=" + params.getString("method") +
				"&" + "nonce_str=" + params.getString("nonce_str") +
				"&" + "name=" + params.getString("name") +
				"&" + "idCard=" + params.getString("idCard") +
				"&" + "sign_type=" + params.getString("sign_type") +
				"&" + "warehouse_no=" + params.getString("warehouse_no") +
				key;
		String sign = DigestUtils.md5Hex(signStr).toUpperCase();

		MultiValueMap<String,String> m = new LinkedMultiValueMap<String, String>();
		m.add("warehouse_no", params.getString("warehouse_no"));
		m.add("charset", params.getString("charset"));
		m.add("customer_no", params.getString("customer_no"));
		m.add("nonce_str", params.getString("nonce_str"));
		m.add("method", params.getString("method"));
		m.add("sign_type", params.getString("sign_type"));
		m.add("name", params.getString("name"));
		m.add("idCard", params.getString("idCard"));
		m.add("sign", sign);
		// 发送请求
		String result = restTemplateUtil.postFormData(url, m);
		log.info("--------------- 公共仓订单查询接口返回, result: {} ---------------", result);
		JSONObject resultObj = JSON.parseObject(result);
		return CommonResult.success(resultObj);
	}

	/**
     * 订单推送(先报支付单, 成功了再推送订单给他们)
     * @author daigb
     * @date:  2022-10-18 15:51
     */
    @Override
    public CommonResult pushOrder(StoreOrder storeOrder, List<StoreOrderInfoVo> infos) {
    	log.info("--------------- 公共仓收到订单推送接口请求, 订单号: {}, 查询仓库: {} ---------------", storeOrder.getOrderId(), "WZBC");
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
    	String signStr = "";
    	// 组装参数
    	JSONObject params = new JSONObject();
    	params.put("warehouse_no", "WZBCN");
    	params.put("charset", charset);
    	params.put("customer_no", customerNo);
    	params.put("nonce_str", UUID.randomUUID().toString().replaceAll("-", ""));
    	params.put("method", "order_add");
    	params.put("sign_type", signType);
    	// 业务信息
    	JSONObject query_params = new JSONObject();
    	query_params.put("order_no", storeOrder.getOrderId());
//    	query_params.put("f_shop_id", "565656");
    	query_params.put("create_time", sdf.format(storeOrder.getCreateTime()));
    	query_params.put("pay_time", sdf.format(storeOrder.getPayTime()));
    	query_params.put("total_price", storeOrder.getPayPrice().movePointRight(2).toString());
    	query_params.put("total_fright", 0);
    	query_params.put("f_discount", 0);
		// 物流信息
		String expressCode = storeOrder.getFeature("expressCode");
		if (StringUtils.isNotBlank(expressCode)){
			query_params.put("logistics_code",expressCode);
		}
		// 报关信息
    	JSONObject customs = new JSONObject();
    	customs.put("idcard", storeOrder.getIdCard());
    	customs.put("idcard_name", StringUtils.defaultString(storeOrder.getPayName(),storeOrder.getRealName()));
    	customs.put("plaform_code", "3303960EZ4"); // 速云海关
		customs.put("order_decla_no", storeOrder.getOrderId()); // 报关单号(就是订单号)
		if (Objects.equals(storeOrder.getRemark(),"BIE_YANG")||storeOrder.getOrderId().startsWith("BY")){
			customs.put("plaform_code", "312224099T");
			customs.put("order_decla_no", storeOrder.getOutOrderId()); // 报关单号(就是订单号)
		}
    	customs.put("pay_id", "310496099C"); // 汇付代码
		if (PAY_TYPE_MAP.containsKey(storeOrder.getPayType())) {
			customs.put("pay_id", PAY_TYPE_MAP.get(storeOrder.getPayType()));
		}
    	customs.put("pay_no", StringUtils.defaultIfBlank(storeOrder.getPayNo(),storeOrder.getOutOrderId()));
    	query_params.put("customs", customs);
    	// 地址信息
    	JSONObject address = new JSONObject();
    	address.put("consignee", storeOrder.getRealName());
    	address.put("consignee_phone", storeOrder.getUserPhone().contains(" ") ? storeOrder.getUserPhone().split(" ")[1] : storeOrder.getUserPhone());
    	address.put("province", storeOrder.getProvince());
    	address.put("city", storeOrder.getCity());
    	address.put("area", StringUtils.isBlank(storeOrder.getDistrict()) ? storeOrder.getCity() : storeOrder.getDistrict());
    	address.put("address", storeOrder.getDetail());
    	query_params.put("address", address);

		Map<String, List<StoreOrderInfoVo>> map = infos.stream().collect(Collectors.groupingBy(k -> k.getInfo().getProductInfo().getBarCode()));

		BigDecimal totalGoodsPrice = BigDecimal.ZERO;
    	// 商品信息
    	JSONArray ja = new JSONArray();
		for (Map.Entry<String, List<StoreOrderInfoVo>> entry : map.entrySet()) {
			String barCode = entry.getKey();
			List<StoreOrderInfoVo> value = entry.getValue();
			int num = 0;
			BigDecimal total = BigDecimal.ZERO;
			for (StoreOrderInfoVo storeOrderInfoVo : value) {
				num = num + storeOrderInfoVo.getInfo().getCartNum();
				total = total.add(storeOrderInfoVo.getPayPrice());
			}
			JSONObject goods = new JSONObject();
			goods.put("goods_no", barCode);
			goods.put("number", num);
			goods.put("total", total.movePointRight(2).toString());
			totalGoodsPrice = totalGoodsPrice.add(total);
			ja.add(goods);
		}
		if (totalGoodsPrice.movePointRight(2).compareTo(storeOrder.getPayPrice().movePointRight(2))!=0){
			BigDecimal subtract = storeOrder.getPayPrice().movePointRight(2).subtract(totalGoodsPrice.movePointRight(2));
			JSONObject obj = ja.getJSONObject(0);
			String total = subtract.add(new BigDecimal(obj.getString("total"))).toString();
			obj.put("total",total);
			DingTalkMsgUtil.sendMsg(DingMsgEnum.WZ_WAREHOUSE,"金额有差异,联系业务确认:"+storeOrder.getOrderId()+"差异:"+subtract);
		}
    	query_params.put("goods", ja);

    	params.put("query_params", query_params);

    	// 组装加签参数
    	signStr = "charset=" + params.getString("charset") +
    			"&" + "customer_no=" + params.getString("customer_no") +
    			"&" + "method=" + params.getString("method") +
    			"&" + "nonce_str=" + params.getString("nonce_str") +
    			"&" + "query_params=" + params.getString("query_params") +
    			"&" + "sign_type=" + params.getString("sign_type") +
    			"&" + "warehouse_no=" + params.getString("warehouse_no") +
    			key;

    	String sign = DigestUtils.md5Hex(signStr).toUpperCase();
    	MultiValueMap<String,String> m = new LinkedMultiValueMap<String, String>();
        m.add("charset", params.getString("charset"));
        m.add("customer_no", params.getString("customer_no"));
        m.add("method", params.getString("method"));
        m.add("nonce_str", params.getString("nonce_str"));
        m.add("query_params", params.getString("query_params"));
        m.add("sign_type", params.getString("sign_type"));
        m.add("warehouse_no", params.getString("warehouse_no"));
        m.add("sign", sign);
		ExecutorService thread = ThreadPoolUtils.getThread();
        log.info("--------------- 公共仓订单推送请求原始参数: {} ---------------", signStr);
		final String msg = "订单推送："+query_params.get("order_no")+": 推送公共中心仓入参: "+signStr;
		thread.execute(new Thread(()->{
			DingTalkMsgUtil.sendMsg(DingMsgEnum.WZ_WAREHOUSE,msg);
		}));
    	// 发送请求
    	String result = restTemplateUtil.postFormData(url, m);
    	log.info("--------------- 公共仓订单推送接口返回, result: {} ---------------", result);
		final String resultMsg = "订单推送："+query_params.get("order_no")+": 推送公共中心仓返回: "+result;
		thread.execute(new Thread(()->{
			DingTalkMsgUtil.sendMsg(DingMsgEnum.WZ_WAREHOUSE,resultMsg);
		}));
    	JSONObject resultObj = JSON.parseObject(result);
    	// 获取对应信息
    	int errorCode = resultObj.getInteger("errorCode");
    	if(0 == errorCode) {
			final String successMsg = "订单："+storeOrder.getOrderId()+"，推送公共中心仓成功";
			thread.execute(new Thread(()->{
				DingTalkMsgUtil.sendMsg(DingMsgEnum.WZ_WAREHOUSE,successMsg);
			}));
        	log.info("--------------- 公共仓订单推送成功, orderNo: {} ---------------", storeOrder.getOrderId());
        	return CommonResult.success();
    	} else {
			final String errorMsg = "订单："+storeOrder.getOrderId()+"，推送公共中心仓失败";
			thread.execute(new Thread(()->{
				DingTalkMsgUtil.sendMsg(DingMsgEnum.WZ_WAREHOUSE,errorMsg);
			}));
        	log.info("--------------- 公共仓订单推送接口异常, message: {} ---------------", resultObj.getString("errorMsg"));
    	}
    	
    	return CommonResult.failed(resultObj.getString("errorMsg"));
    }
    
    /**
     * 订单物流查询
     * @author daigb
     * @date:  2022-10-18 16:06
     */
    @Override
    public JSONObject queryOrder(String orderNo) {
    	log.info("--------------- 公共仓收到订单查询接口请求, 订单号: {}, 查询仓库: WZBC ---------------", orderNo);
    	String signStr = "";
    	
    	JSONObject params = new JSONObject();
    	params.put("warehouse_no", "WZBCN");
    	params.put("charset", charset);
    	params.put("customer_no", customerNo);
    	params.put("nonce_str", UUID.randomUUID().toString().replaceAll("-", ""));
    	params.put("method", "order_query");
    	params.put("sign_type", signType);
    	params.put("order_no", orderNo);
    	
    	// 组装加签参数
    	signStr = "charset=" + params.getString("charset") + 
    			"&" + "customer_no=" + params.getString("customer_no") + 
    			"&" + "method=" + params.getString("method") + 
    			"&" + "nonce_str=" + params.getString("nonce_str") + 
    			"&" + "order_no=" + params.getString("order_no") + 
    			"&" + "sign_type=" + params.getString("sign_type") + 
    			"&" + "warehouse_no=" + params.getString("warehouse_no") + 
    			key;
    	String sign = DigestUtils.md5Hex(signStr).toUpperCase();

        MultiValueMap<String,String> m = new LinkedMultiValueMap<String, String>();
        m.add("warehouse_no", params.getString("warehouse_no"));
        m.add("charset", params.getString("charset"));
        m.add("customer_no", params.getString("customer_no"));
        m.add("nonce_str", params.getString("nonce_str"));
        m.add("method", params.getString("method"));
        m.add("sign_type", params.getString("sign_type"));
        m.add("order_no", params.getString("order_no"));
        m.add("sign", sign);
    	// 发送请求
    	String result = restTemplateUtil.postFormData(url, m);
    	log.info("--------------- 公共仓订单查询接口返回, result: {} ---------------", result);
    	JSONObject resultObj = JSON.parseObject(result);
    	// 获取对应信息
    	int errorCode = resultObj.getInteger("errorCode");
    	if(0 == errorCode) {
    		JSONObject resultData = resultObj.getJSONObject("data");
     		// (0,"待审核" 3,"待支付" 15,"清关中" 20,"清关完成" 30,"下发仓库" 40,"订单出库" 50,"订单取消" 100,"已完成")
//    		Integer orderInfoStatus = resultData.getInteger("orderInfoStatus"); // 订单状态
    		// 圆通   YTO 韵达   YUNDA 中通  ZTO 顺丰  SF 邮政   YOUZHENG
//    		String expressId = resultData.getString("expressId"); // 快递简称
//    		String expressNo = resultData.getString("expressNo"); // 快递单号
//    		Integer intercept = resultData.getInteger("intercept"); // 拦截信息, 不为0代表被拦截

    		return resultData;
    	} else {
        	log.info("--------------- 凯仓订单查询接口异常, message: {} ---------------", resultObj.getString("errorMsg"));
    	}
    	
    	return null;
    }


	@Override
	public CommonResult<List<WarehouseResultData>> queryOrderStockInfo(String goodsNos) {

		// 组装参数
		JSONObject params = new JSONObject();
		params.put("warehouse_no", "YQYZY");
		params.put("charset", charset);
		params.put("customer_no", customerNo);
		params.put("nonce_str", UUID.randomUUID().toString().replaceAll("-", ""));
		params.put("method", "inventory");
		params.put("sign_type", signType);

		params.put("query_params", goodsNos);


		// 组装加签参数
		String signStr = "charset=" + params.getString("charset") +
				"&" + "customer_no=" + params.getString("customer_no") +
				"&" + "method=" + params.getString("method") +
				"&" + "nonce_str=" + params.getString("nonce_str") +
				"&" + "query_params=" + params.getString("query_params") +
				"&" + "sign_type=" + params.getString("sign_type") +
				"&" + "warehouse_no=" + params.getString("warehouse_no") +
				key;
		String sign = DigestUtils.md5Hex(signStr).toUpperCase();

		MultiValueMap<String,String> m = new LinkedMultiValueMap<String, String>();
		m.add("warehouse_no", params.getString("warehouse_no"));
		m.add("charset", params.getString("charset"));
		m.add("customer_no", params.getString("customer_no"));
		m.add("nonce_str", params.getString("nonce_str"));
		m.add("method", params.getString("method"));
		m.add("sign_type", params.getString("sign_type"));
		m.add("query_params", params.getString("query_params"));
		m.add("sign", sign);
		// 发送请求 {"data":{"remaining":26000},"errorCode":0,"errorMsg":"success"}
		String result = restTemplateUtil.postFormData(url, m);
		log.info("--------------- 公共仓订单查询接口返回, result: {} ---------------", result);
		JSONObject resultObj = JSON.parseObject(result);
		String data = resultObj.getString("data");
		List<WarehouseResultData> warehouseResultData = JSON.parseArray(data, WarehouseResultData.class);
		return CommonResult.success(warehouseResultData);

	}


	public static void main(String[] args) {
		BigDecimal total = new BigDecimal("123.32");
		BigDecimal totalGoodsPrice = BigDecimal.ZERO;
		JSONObject goods = new JSONObject();
		goods.put("total", total.movePointRight(2).toString());
		totalGoodsPrice = totalGoodsPrice.add(total);
		JSONArray ja = new JSONArray();
		ja.add(goods);
		if (totalGoodsPrice.movePointRight(2).compareTo(new BigDecimal("124.32").movePointRight(2))!=0){
			BigDecimal subtract = totalGoodsPrice.movePointRight(2).subtract(new BigDecimal("124.32").movePointRight(2));
			JSONObject obj = ja.getJSONObject(0);
			String total1 = subtract.add(new BigDecimal(obj.getString("total"))).toString();
			obj.put("total",total1);
			System.out.println(JSON.toJSONString(ja));
			return;
		}



		String url = "http://api.jmcompany.cn/wzbc/api/openapi/main.html";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
		String customerNo = "jhy3nrg2ugoxwsov"; //客户编码
		String key = "qjvlulgnldfgix5w4vt2s876yfsqz9an"; // 秘钥
		String signStr = "";

		JSONObject params = new JSONObject();
		params.put("warehouse_no", "WZBCN");
		params.put("charset", "UTF-8");
		params.put("customer_no", customerNo);
		params.put("nonce_str", UUID.randomUUID().toString().replaceAll("-", ""));
		params.put("method", "order_query");
		params.put("sign_type", "MD5");
		params.put("order_no", "FD2023122711112058247");

		// 组装加签参数
		signStr = "charset=" + params.getString("charset") +
				"&" + "customer_no=" + params.getString("customer_no") +
				"&" + "method=" + params.getString("method") +
				"&" + "nonce_str=" + params.getString("nonce_str") +
				"&" + "order_no=" + params.getString("order_no") +
				"&" + "sign_type=" + params.getString("sign_type") +
				"&" + "warehouse_no=" + params.getString("warehouse_no") +
				key;
		String sign = DigestUtils.md5Hex(signStr).toUpperCase();

		MultiValueMap<String,String> m = new LinkedMultiValueMap<String, String>();
		m.add("warehouse_no", params.getString("warehouse_no"));
		m.add("charset", params.getString("charset"));
		m.add("customer_no", params.getString("customer_no"));
		m.add("nonce_str", params.getString("nonce_str"));
		m.add("method", params.getString("method"));
		m.add("sign_type", params.getString("sign_type"));
		m.add("order_no", params.getString("order_no"));
		m.add("sign", sign);
		// 发送请求
		HttpHeaders headers = new HttpHeaders();
		HttpEntity<MultiValueMap<String, String>> requests = new HttpEntity<MultiValueMap<String, String>>(m, headers);
		String result = new RestTemplate().postForEntity(url, requests, String.class).getBody();
		log.info("--------------- 凯仓订单查询接口返回, result: {} ---------------", result);
		JSONObject resultObj = JSON.parseObject(result);
		System.out.println();


//    	String url = "http://api.yiwukaicang.com/ywkc/api/openapi/main";
//    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
//    	String customerNo = "ZJSY221208"; //客户编码
//    	String key = "089ED7FFE113EC4798D6A4CFC240D612"; // 秘钥
//    	String signStr = "";
//
//    	JSONObject params = new JSONObject();
//    	params.put("warehouse_no", "WZBC");
//    	params.put("charset", "UTF-8");
//    	params.put("customer_no", customerNo);
//    	params.put("nonce_str", UUID.randomUUID().toString().replaceAll("-", ""));
//    	params.put("method", "order_query");
//    	params.put("sign_type", "MD5");
//    	params.put("order_no", "FD2022120909473334772");
//
//    	// 组装加签参数
//    	signStr = "charset=" + params.getString("charset") +
//    			"&" + "customer_no=" + params.getString("customer_no") +
//    			"&" + "method=" + params.getString("method") +
//    			"&" + "nonce_str=" + params.getString("nonce_str") +
//    			"&" + "order_no=" + params.getString("order_no") +
//    			"&" + "sign_type=" + params.getString("sign_type") +
//    			"&" + "warehouse_no=" + params.getString("warehouse_no") +
//    			key;
//    	String sign = DigestUtils.md5Hex(signStr).toUpperCase();
//
//        MultiValueMap<String,String> m = new LinkedMultiValueMap<String, String>();
//        m.add("warehouse_no", params.getString("warehouse_no"));
//        m.add("charset", params.getString("charset"));
//        m.add("customer_no", params.getString("customer_no"));
//        m.add("nonce_str", params.getString("nonce_str"));
//        m.add("method", params.getString("method"));
//        m.add("sign_type", params.getString("sign_type"));
//        m.add("order_no", params.getString("order_no"));
//        m.add("sign", sign);
//    	// 发送请求
//		HttpHeaders headers = new HttpHeaders();
//		HttpEntity<MultiValueMap<String, String>> requests = new HttpEntity<MultiValueMap<String, String>>(m, headers);
//		String result = new RestTemplate().postForEntity(url, requests, String.class).getBody();
//    	log.info("--------------- 凯仓订单查询接口返回, result: {} ---------------", result);
//    	JSONObject resultObj = JSON.parseObject(result);
		// 获取对应信息
		int errorCode = resultObj.getInteger("errorCode");
		if(0 == errorCode) {
			JSONObject resultData = resultObj.getJSONObject("data");
			// (0,"待审核" 3,"待支付" 15,"清关中" 20,"清关完成" 30,"下发仓库" 40,"订单出库" 50,"订单取消" 100,"已完成")
			Integer orderInfoStatus = resultData.getInteger("orderInfoStatus"); // 订单状态
			// 圆通   YTO 韵达   YUNDA 中通  ZTO 顺丰  SF 邮政   YOUZHENG
			String expressId = resultData.getString("expressId"); // 快递简称
			String expressNo = resultData.getString("expressNo"); // 快递单号
			Integer intercept = resultData.getInteger("intercept"); // 拦截信息, 不为0代表被拦截

		} else {
			log.info("--------------- 凯仓订单查询接口异常, message: {} ---------------", resultObj.getString("errorMsg"));
		}

		// 组装参数
		/*JSONObject params = new JSONObject();
		params.put("warehouse_no", "WZBC");
		params.put("charset", "UTF-8");
		params.put("customer_no", customerNo);
		params.put("nonce_str", UUID.randomUUID().toString().replaceAll("-", ""));
		params.put("method", "order_add");
		params.put("sign_type", "MD5");
		// 业务信息
		JSONObject query_params = new JSONObject();
		query_params.put("order_no", "FD2022120909473334772");
		//    	query_params.put("f_shop_id", "565656");
		query_params.put("create_time", "2022-12-09 09:47:42");
		query_params.put("pay_time", "2022-12-09 09:47:42");
		query_params.put("total_price", "98600");
		query_params.put("total_fright", 0);
		query_params.put("f_discount", 0);
		// 报关信息
		JSONObject customs = new JSONObject();
		customs.put("idcard", "330382199711036615");
		customs.put("idcard_name", "金豪杰");
		customs.put("plaform_code", "3303960EZ4"); // 速云海关
		customs.put("pay_id", "310496099C"); // 汇付代码
		customs.put("pay_no", "2032723247");
		customs.put("order_decla_no", "FD2022120909473334772"); // 报关单号
		query_params.put("customs", customs);
		// 地址信息
		JSONObject address = new JSONObject();
		address.put("consignee", "金豪杰");
		address.put("consignee_phone", "13989773877");
		address.put("province", "浙江省");
		address.put("city", "温州市");
		address.put("area", "瓯海区");
		address.put("address", "娄桥街道广丰大厦702");
		query_params.put("address", address);
		// 商品信息
		JSONObject goods = new JSONObject();
		goods.put("goods_no", "ZJSY51160123060021");
		goods.put("number", 1);
		goods.put("total", "98600");
		query_params.put("goods", goods);

		params.put("query_params", query_params);

		// 组装加签参数
		signStr = "charset=" + params.getString("charset") +
				"&" + "customer_no=" + params.getString("customer_no") +
				"&" + "method=" + params.getString("method") +
				"&" + "nonce_str=" + params.getString("nonce_str") +
				"&" + "query_params=" + params.getString("query_params") +
				"&" + "sign_type=" + params.getString("sign_type") +
				"&" + "warehouse_no=" + params.getString("warehouse_no") +
				key;

		String sign = DigestUtils.md5Hex(signStr).toUpperCase();
		MultiValueMap<String,String> m = new LinkedMultiValueMap<String, String>();
		m.add("charset", params.getString("charset"));
		m.add("customer_no", params.getString("customer_no"));
		m.add("method", params.getString("method"));
		m.add("nonce_str", params.getString("nonce_str"));
		m.add("query_params", params.getString("query_params"));
		m.add("sign_type", params.getString("sign_type"));
		m.add("warehouse_no", params.getString("warehouse_no"));
		m.add("sign", sign);
		// 发送请求
		HttpHeaders headers = new HttpHeaders();
		HttpEntity<MultiValueMap<String, String>> requests = new HttpEntity<MultiValueMap<String, String>>(m, headers);
		String result = new RestTemplate().postForEntity(url, requests, String.class).getBody();
		log.info("--------------- 凯仓订单推送接口返回, result: {} ---------------", result);
		JSONObject resultObj = JSON.parseObject(result);
		// 获取对应信息
		int errorCode = resultObj.getInteger("errorCode");
		if(0 == errorCode) {
			log.info("--------------- 凯仓订单推送成功, orderNo: {} ---------------", "FD2022120909473334772");
		} else {
			log.info("--------------- 凯仓订单推送接口异常, message: {} ---------------", resultObj.getString("errorMsg"));
		}*/
	}
    
}
